#pragma once

#include <iostream>
#include <vector>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include "Util.hpp"
#include "Log.hpp"

class HttpRequest
{
public:
    std::string request_line; // 请求行
    std::vector<std::string> request_header; // 请求报头
    std::string blank;
    std::string request_body; // 请求报文
};

class HttpResponce
{
public:
    std::string status_line; 
    std::vector<std::string> responce_header;
    std::string blank;
    std::string responce_body;
};

// 读取请求，分析请求，构建相应
// IO通信
class EndPoint
{
private:
    int sock;
    HttpRequest http_request;
    HttpResponce http_responce;
private:
    void RecvHttpRequestLine()
    {
        Util::ReadLine(sock,http_request.request_line);
    }

    void RecvHttpRequestHeader()
    {

    }
public:
    EndPoint(int _sock)
        : sock(_sock)
    {
    }

    void RcvRequest()
    {

    }

    void ParseResponce()
    {
    }

    void BuildResponce()
    {
    }

    void SendResponce()
    {
    }
    ~EndPoint()
    {
    }
};

// 入口
class Entrance
{
public:
    static void *HandlerRequest(void *_sock)
    {
        int sock = *(int *)_sock;
        delete (int *)_sock;
        std::string out;
        ssize_t s = Util::ReadLine(sock, out);
        if (s > 0)
        {
            std::cout << out << std::endl;
        }
        else if (s == 0)
        {
            std::string message = std::to_string(sock);
            message += "：链接已关闭";
            LOG(INFO,message);
        }
        else
        {
            LOG(ERROR,"Read error.....");
        }
        close(sock);
        return nullptr;
    }
};